//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System;
using System.Xml.Serialization;
 
namespace Microsoft.TridentWordAddIn.Common
{
    public class RerunJob : DomainBase
    {
        private TridentConnection rerunConnection;
        private bool promptForReconnect = true;

        /// <summary>
        /// Initializes a new instance of the <see cref="RerunJob"/> class.
        /// </summary>
        public RerunJob()
        {
            this.Job = new WorkflowJob();
        }

        /// <summary>
        /// Gets or sets the job.
        /// </summary>
        /// <value>The job.</value>
        public WorkflowJob Job { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [result inserted].
        /// </summary>
        /// <value><c>true</c> if [result inserted]; otherwise, <c>false</c>.</value>
        public bool ResultInserted { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is output present.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is output present; otherwise, <c>false</c>.
        /// </value>
        public bool IsOutputPresent { get; set; }

        /// <summary>
        /// Gets the job id.
        /// </summary>
        /// <value>The job id.</value>
        public Guid JobId
        {
            get
            {
                if (this.Job != null)
                {
                    return this.Job.Id;
                }

                return Guid.Empty;
            }
        }

        /// <summary>
        /// Gets or sets the rerun connection.
        /// </summary>
        /// <value>The rerun connection.</value>
        [XmlIgnore]
        [DoNotCompare]
        [DoNotClone]
        public TridentConnection Connection
        {
            get
            {
                return rerunConnection;
            }
            set
            {
                rerunConnection = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [prompt for reconnect].
        /// </summary>
        /// <value><c>true</c> if [prompt for reconnect]; otherwise, <c>false</c>.</value>
        public bool PromptForReconnect { get { return promptForReconnect; } set { promptForReconnect = value; } }
      
        /// <summary>
        /// Gets the job status.
        /// </summary>
        /// <value>The job status.</value>
        public WorkflowJobStatus JobStatus
        {
            get
            {
                if (this.Job != null)
                {
                    return this.Job.Status;
                }

                return WorkflowJobStatus.Unknown;
            }
        }

        /// <summary>
        /// Gets the name of the job ouput file.
        /// </summary>
        /// <param name="jobId">The job id.</param>
        /// <returns></returns>
        public string GetJobOutputFileName(Guid jobId)
        {
            string fileName = string.Empty;
            if (this.Job != null)
            {
                foreach (ShallowJobOutput output in this.Job.Outputs)
                {
                    if (output.Id == jobId)
                    {
                        fileName = output.Name + output.Extension;
                        break;
                    }
                }
            }

            return fileName;
        }
    }
}
